package com.lakeworks.common.enums;

import com.alibaba.fastjson.JSONObject;
import com.lakeworks.common.exception.response.ResponseException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.HashMap;

import static java.util.Arrays.stream;

/**
 * @Author: Auxps
 * @Date: 2023/3/24
 * Description:
 */
@Getter
@AllArgsConstructor
public enum ChannelErrorEnum {

    PICKUPNO_BLANK("40113", "入厂单号为空"),
    PICKUPNO_INVALID("40114", "入厂单号非法"),
    PICKUP_NOT_FIND("40123", "入厂单号不存在"),
    PICKUP_NOT_CANCEL("40133", "不能取消入厂单"),
    SHIPMENTNO_BLANK("40213", "出厂单号为空"),
    SHIPMENTNO_INVALID("40214", "出厂单号非法"),
    SHIPMENT_NOT_FIND("40223", "出厂单号不存在"),
    CARRIER_ERROR("40313", "物流商不存在"),
    EXPRESS_BLANK("40323", "运单号为空"),
    EXPRESS_INVALID("40324", "运单号非法"),
    EXPRESS_ERROR("40325", "运单号有误"),
    LABEL_ERROR("40334", "面单地址为空"),
    BARCODE_ERROR("40413", "水洗码有误"),
    BARCODE_REPEAT_ALLOCATE("40414", "衣物重复分配出厂单"),
    MERCHANDISE_NUM_ERROR("40513", "商品数量有误"),
    MERCHANDISE_ITEMS_BLANK("40514", "商品列表为空"),
    MERCHANDISE_CODE_BLANK("40515", "商品编码为空"),
    MERCHANDISE_NAME_BLANK("40516", "商品名不能为空"),
    BARCODE_ITEMS_BLANK("40517", "水洗码列表为空"),
    SYSPRODUCT_NOT_FIND("40121", "系统产品不存在"),
    NO_PUTWAY("40637", "产品未上架（未到下发物流时间）"),
    PARAM_ERROR("40700","参数错误");

    private final String errcode;

    private final String errmsg;

    private static final HashMap<String, ChannelErrorEnum> errorHashMap = new HashMap<>(32);

    static {
        stream(values()).forEach(channelErrorEnum -> errorHashMap.put(channelErrorEnum.getErrcode(), channelErrorEnum));
    }

    public static String message(String code) {
        if (!errorHashMap.containsKey(code.trim()))
            throw new ResponseException("errcode不存在");
        return errorHashMap.get(code.trim()).getErrmsg();
    }

    public static JSONObject byCode(String code) {
        if (!errorHashMap.containsKey(code.trim()))
            throw new ResponseException("errcode不存在");

        JSONObject jsonObject = new JSONObject();

        ChannelErrorEnum channelErrorEnum = errorHashMap.get(code.trim());
        jsonObject.put("errcode", channelErrorEnum.getErrcode());
        jsonObject.put("errmsg", channelErrorEnum.getErrmsg());

        return jsonObject;
    }
}
